package com.cogent.business.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cogent.common.util.Globals;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.menu.cache.MenuCache;
import com.cogent.menu.domain.MenuDomain;


public class MenuHelper {
	public static List<MenuDomain> getFirstMenu() {
		return MenuCache.getFirstMenu();
	}
	
	public static List<MenuDomain> getFirstMenu(HttpServletRequest request) {
		List list = MenuCache.getFirstMenu();
		List<MenuDomain> retList = new ArrayList<MenuDomain>();
		Iterator it = list.iterator();
		while(it.hasNext()) {
			MenuDomain menudomain = (MenuDomain)it.next();
			if(UserDomainHelper.isAllow(ServletActionContext.getRequest(), menudomain.getResourcecode(), Globals.VIEW))
				retList.add(menudomain);
		}
		
		return retList;
	}
	
	public static List<MenuDomain> getSubMenu(String key) {
		return MenuCache.getSubMenu(key);
	}
	
	public static List<MenuDomain> getSubMenu(HttpServletRequest request, String key) {
		List list = MenuCache.getSubMenu(key);
		List<MenuDomain> retList = new ArrayList<MenuDomain>();
		Iterator it = list.iterator();
		while(it.hasNext()) {
			MenuDomain menudomain = (MenuDomain)it.next();
			if(UserDomainHelper.isAllow(ServletActionContext.getRequest(), menudomain.getResourcecode(), Globals.VIEW))
				retList.add(menudomain);
		}
		
		return retList;
	}
	
	public static String getSubFirstUrl(HttpServletRequest request, String key) {
		List list = MenuCache.getSubMenu(key);
		List<MenuDomain> retList = new ArrayList<MenuDomain>();
		Iterator it = list.iterator();
		while(it.hasNext()) {
			MenuDomain menudomain = (MenuDomain)it.next();
			if(UserDomainHelper.isAllow(ServletActionContext.getRequest(), menudomain.getResourcecode(), Globals.VIEW)) {
				if(menudomain.getLevels().equals("3"))
					return menudomain.getResourcepath();
				else
					return getSubFirstUrl(request, menudomain.getResourcecode());
			}
		}
		
		return "";
	}
	
	public static List<MenuDomain> getThridMenu(HttpServletRequest request, String key) {
		List list = MenuCache.getSubMenu(key);
		List<MenuDomain> retList = new ArrayList<MenuDomain>();
		Iterator it = list.iterator();
		while(it.hasNext()) {
			MenuDomain menudomain = (MenuDomain)it.next();
			if(UserDomainHelper.isAllow(ServletActionContext.getRequest(), menudomain.getResourcecode(), Globals.VIEW)) {
				if(menudomain.getLevels().equals("3"))
					retList.add(menudomain);
				else
					return getThridMenu(request, menudomain.getResourcecode());
			}
		}
		
		return retList;
	} 
	
	public static String getMenu(HttpServletRequest request, String resourcecode) {
		return getMenu(request, resourcecode, "");
	}
	
	public static String getPermissionId(HashMap permissionmap, String recid) {
		String ret = "";
		if(permissionmap.containsKey(recid)) {
			HashMap curMap = (HashMap)permissionmap.get(recid);
			ret = MapUtil.getStringValue(curMap, "id");
		}
		
		return ret;
	}
	
	public static String genPermissionCheckBox(HashMap permissionmap, MenuDomain menudomain, int num, String recid) {
		StringBuffer str = new StringBuffer();
		if(permissionmap.containsKey(recid)) {
			HashMap curMap = (HashMap)permissionmap.get(recid);
			int value = MapUtil.getIntValue(curMap, "permissioncode", 0);
			for(int i=0; i<7; i++) {
				int curValue = (int)Math.pow(2, i);
				if((value & curValue) == curValue)
					str.append("<td  class='td_label'><input type=\"checkbox\" id=\"val_" + num + "_" + i + "\" name=\"val_" + num + "_" + i + "\" value=\"true\" checked/></td>");
				else {
					int temVale = StringUtil.toInt(menudomain.getPrivilege(), 0);
					if((temVale & curValue) == curValue)
						str.append("<td  class='td_label'><input type=\"checkbox\" id=\"val_" + num + "_" + i + "\" name=\"val_" + num + "_" + i + "\" value=\"true\"/></td>");
					else
						str.append("<td  class='td_label'>&nbsp;</td>");
				}
			}
		} else {
			for(int i=0; i<7; i++) {
				int curValue = (int)Math.pow(2, i);
				int temVale = StringUtil.toInt(menudomain.getPrivilege(), 0);
				if((temVale & curValue) == curValue)
					str.append("<td  class='td_label'><input type=\"checkbox\" id=\"val_" + num + "_" + i + "\" name=\"val_" + num + "_" + i + "\" value=\"true\"/></td>");
				else
					str.append("<td  class='td_label'>&nbsp;</td>");
			}
		}
		return str.toString();
	}
	
	public static String genPermissionCheckBoxView(HashMap permissionmap, MenuDomain menudomain, int num, String recid) {
		StringBuffer str = new StringBuffer();
		if(permissionmap.containsKey(recid)) {
			HashMap curMap = (HashMap)permissionmap.get(recid);
			int value = MapUtil.getIntValue(curMap, "permissioncode", 0);
			for(int i=0; i<7; i++) {
				int curValue = (int)Math.pow(2, i);
				if((value & curValue) == curValue)
					str.append("<td  class='td_label'><input type=\"checkbox\" id=\"val_" + num + "_" + i + "\" name=\"val_" + num + "_" + i + "\" value=\"true\" checked disabled=\"true\"/></td>");
				else {
					int temVale = StringUtil.toInt(menudomain.getPrivilege(), 0);
					if((temVale & curValue) == curValue)
						str.append("<td  class='td_label'><input type=\"checkbox\" id=\"val_" + num + "_" + i + "\" name=\"val_" + num + "_" + i + "\" value=\"true\" disabled=\"true\"/></td>");
					else
						str.append("<td  class='td_label'>&nbsp;</td>");
				}
			}
		} else {
			for(int i=0; i<7; i++) {
				int curValue = (int)Math.pow(2, i);
				int temVale = StringUtil.toInt(menudomain.getPrivilege(), 0);
				if((temVale & curValue) == curValue)
					str.append("<td  class='td_label'><input type=\"checkbox\" id=\"val_" + num + "_" + i + "\" name=\"val_" + num + "_" + i + "\" value=\"true\" disabled=\"true\"/></td>");
				else
					str.append("<td  class='td_label'>&nbsp;</td>");
			}
		}
		return str.toString();
	}
	
	public static String getMenu(HttpServletRequest request, String resourcecode, String curcode) {
		StringBuffer str = new StringBuffer();
		str.append("<div id=\"head_bar\">").append(" <table height=\"34px\" class=\"table_head_bar\">")
			.append("<tr valign=\"middle\">").append("<td width=\"10px\"></td>");
		List list = MenuCache.getSubMenu(resourcecode);
		Iterator it = list.iterator();
		boolean isfrist = true;
		String classname = "css_btnbg";
		while(it.hasNext()) {
			MenuDomain menuDomain = (MenuDomain)it.next();
			if((StringUtil.isEmpty(curcode) && (isfrist)) || (curcode.equals(menuDomain.getResourcecode())))
				classname = "css_btnNonce";
			else
				classname = "css_btnbg";
			str.append("<td><input");
			if(!UserDomainHelper.isAllow(request, menuDomain.getResourcecode(), Globals.VIEW))
				str.append(" disabled");
			else
				isfrist = false;
			str.append(" type=\"button\" name =\"topBtn22\" class=\"").append(classname).append("\" value=\"").append(menuDomain.getResourcename())
				.append("\" onClick=\"location.href='").append(menuDomain.getResourcepath()).append("'\"></td>");
		}
		str.append("<td width=\"100%\"></td>").append("</tr></table></div>");
		
		return str.toString();
	}
	
	public static String getMenuByCurCode(HttpServletRequest request, String curcode) {
		StringBuffer str = new StringBuffer();
		str.append("<div id=\"head_bar\">").append(" <table height=\"34px\" class=\"table_head_bar\">")
			.append("<tr valign=\"middle\">").append("<td width=\"10px\"></td>");
		MenuDomain curmenuDomain = MenuCache.getMenuInfoByKey(curcode);
		List list = MenuCache.getSubMenu(curmenuDomain.getParentMenuDomain().getResourcecode());
		Iterator it = list.iterator();
		boolean isfrist = true;
		String classname = "css_btnbg";
		while(it.hasNext()) {
			MenuDomain menuDomain = (MenuDomain)it.next();
			if((StringUtil.isEmpty(curcode) && (isfrist)) || (curcode.equals(menuDomain.getResourcecode())))
				classname = "css_btnNonce";
			else
				classname = "css_btnbg";
			str.append("<td><input");
			if(!UserDomainHelper.isAllow(request, menuDomain.getResourcecode(), Globals.VIEW))
				str.append(" disabled");
			else
				isfrist = false;
			str.append(" type=\"button\" name =\"topBtn22\" class=\"").append(classname).append("\" value=\"").append(menuDomain.getResourcename())
				.append("\" onClick=\"location.href='").append(menuDomain.getResourcepath()).append("'\"></td>");
		}
		str.append("<td width=\"100%\"></td>").append("</tr></table></div>");
		
		return str.toString();
	}
	
}
